# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ///////////////////////////////////////////////////////////////
# @File    :   jpgToPng.py
# @Time    :   2024/08/15 15:25:55
# @Author  :   kai.luo
# @Version :   1.0.0
# @Desc    :   Convert JPG format images to PNG format
# ///////////////////////////////////////////////////////////////

import os
from PIL import Image


class Jpg2Png(object):
    def __init__(self) -> None:
        pass

    def convert_jpg_to_png(self, img_jpg_dir, img_png_dir):
        for root, dirs, files in os.walk(img_jpg_dir):
            for file in files:
                jpg_path = os.path.join(root, file)
                try:
                    # 打开 JPG 图像文件
                    with Image.open(jpg_path) as img:
                        # 转换为 PNG 格式
                        png_path = os.path.join(img_png_dir, file.rsplit('.', 1)[0] + '.png')
                        img.save(png_path, 'PNG')
                except:
                    print(f'图片{jpg_path}转png格式失败！')
